package com.liuzhongni.wahahanews.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.liuzhongni.wahahanews.R;
import com.liuzhongni.wahahanews.singleton.VolleySingleton;

import java.util.ArrayList;

import com.liuzhongni.wahahanews.pictureclass.PictureDatails;

/**
 * Created by dllo on 16/1/21.
 */
public class RecyclerViewAdapterOfPicture extends RecyclerView.Adapter {
    private Context context;
    private ArrayList<PictureDatails> data;

    public RecyclerViewAdapterOfPicture(Context context, ArrayList<PictureDatails> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 1) {
            View view = LayoutInflater.from(context).inflate(R.layout.new_picture_type1,null);
            PictureViewHolderType1 holderType1 = new PictureViewHolderType1(view);
            return holderType1;

        } else if (viewType == 2) {
            View view = LayoutInflater.from(context).inflate(R.layout.new_picture_type2,null);
            PictureViewHolderType2 holderType2 = new PictureViewHolderType2(view);
            return holderType2;


        } else if (viewType == 3) {
            View view = LayoutInflater.from(context).inflate(R.layout.new_picture_type3,null);
            PictureViewHolderType3 holderType3 = new PictureViewHolderType3(view);
            return holderType3;

        }

        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int index = holder.getLayoutPosition();
        if(getItemViewType(position) == 1){

            VolleySingleton.getOurInstance().imgJson(((PictureViewHolderType1)holder).imageView,data.get(index).getCover());
            ((PictureViewHolderType1)holder).textViewName.setText(data.get(index).getSetname());
            ((PictureViewHolderType1)holder).textViewPictureNum.setText(data.get(index).getImgsum() + "pics");
            ((PictureViewHolderType1)holder).textViewReplyNum.setText(data.get(index).getReplynum()+"跟帖");
        }else if (getItemViewType(index) == 2) {
            Log.d("RecyclerViewAdapterOfPi", "position:" + position);
            Log.d("RecyclerViewAdapterOfPi", "data.size():" + data.size());
            Log.d("RecyclerViewAdapterOfPi", "data.get(position).getPics().size():" + data.get(index).getPics().size());
            if(data.get(index).getPics().size()>0){

                VolleySingleton.getOurInstance().imgJson(((PictureViewHolderType2) holder).imageViewLeft, data.get(index).getPics().get(0));
                VolleySingleton.getOurInstance().imgJson(((PictureViewHolderType2) holder).imageViewBottom, data.get(index).getPics().get(1));
                VolleySingleton.getOurInstance().imgJson(((PictureViewHolderType2) holder).imageViewTop, data.get(index).getPics().get(2));
            }else {
                ((PictureViewHolderType2) holder).imageViewLeft.setImageResource(R.mipmap.iconfont_jinxingzhongnew);
                ((PictureViewHolderType2) holder).imageViewBottom.setImageResource(R.mipmap.iconfont_jinxingzhongnew);
                ((PictureViewHolderType2) holder).imageViewTop.setImageResource(R.mipmap.iconfont_jinxingzhongnew);
            }
            ((PictureViewHolderType2) holder).textViewName.setText(data.get(index).getSetname());
            ((PictureViewHolderType2) holder).textViewPictureNum.setText(data.get(index).getImgsum() + "pics");
            ((PictureViewHolderType2) holder).textViewReplyNum.setText(data.get(index).getReplynum() + "跟帖");
        }else if (getItemViewType(index) == 3){
            if(data.get(index).getPics().size()>0){

                VolleySingleton.getOurInstance().imgJson(((PictureViewHolderType3)holder).imageViewRight,data.get(index).getPics().get(0));
                VolleySingleton.getOurInstance().imgJson(((PictureViewHolderType3)holder).imageViewBottom,data.get(index).getPics().get(1));
                VolleySingleton.getOurInstance().imgJson(((PictureViewHolderType3)holder).imageViewTop,data.get(index).getPics().get(2));
            }else {
                ((PictureViewHolderType3) holder).imageViewRight.setImageResource(R.mipmap.iconfont_jinxingzhongnew);
                ((PictureViewHolderType3) holder).imageViewBottom.setImageResource(R.mipmap.iconfont_jinxingzhongnew);
                ((PictureViewHolderType3) holder).imageViewTop.setImageResource(R.mipmap.iconfont_jinxingzhongnew);
            }
            ((PictureViewHolderType3)holder).textViewName.setText(data.get(index).getSetname());
            ((PictureViewHolderType3)holder).textViewPictureNum.setText(data.get(index).getImgsum() + "pics");
            ((PictureViewHolderType3)holder).textViewReplyNum.setText(data.get(index).getReplynum()+"跟帖");

        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            return 1;
        } else if (position % 2 == 1) {
            if (((position - 1) / 2) % 2 == 0) {
                return 2;
            } else if (((position - 1) / 2) % 2 == 1) {
                return 3;
            }
        }
        return Integer.parseInt(null);
    }

    public class PictureViewHolderType1 extends RecyclerView.ViewHolder{
        private NetworkImageView imageView;
        private TextView textViewPictureNum;
        private TextView textViewName;
        private TextView textViewReplyNum;

        public PictureViewHolderType1(View view) {
            super(view);
            imageView = (NetworkImageView) view.findViewById(R.id.picture_type1_iv);
            textViewPictureNum = (TextView) view.findViewById(R.id.picture_type1_tv_counts);
            textViewName = (TextView) view.findViewById(R.id.picture_type1_tv_name);
            textViewReplyNum = (TextView) view.findViewById(R.id.picture_type1_tv_gentie);
        }
    }

    public class PictureViewHolderType2 extends RecyclerView.ViewHolder{
        private NetworkImageView imageViewLeft;
        private NetworkImageView imageViewTop;
        private NetworkImageView imageViewBottom;
        private TextView textViewPictureNum;
        private TextView textViewName;
        private TextView textViewReplyNum;

        public PictureViewHolderType2(View view) {
            super(view);
            imageViewLeft = (NetworkImageView) view.findViewById(R.id.picture_type2_iv_left);
            imageViewTop = (NetworkImageView) view.findViewById(R.id.picture_type2_iv_top);
            imageViewBottom = (NetworkImageView) view.findViewById(R.id.picture_type2_iv_bottom);
            textViewPictureNum = (TextView) view.findViewById(R.id.picture_type2_tv_counts);
            textViewName = (TextView) view.findViewById(R.id.picture_type2_tv_name);
            textViewReplyNum = (TextView) view.findViewById(R.id.picture_type2_tv_gentie);
        }
    }

    public class PictureViewHolderType3 extends RecyclerView.ViewHolder{
        private NetworkImageView imageViewRight;
        private NetworkImageView imageViewTop;
        private NetworkImageView imageViewBottom;
        private TextView textViewPictureNum;
        private TextView textViewName;
        private TextView textViewReplyNum;

        public PictureViewHolderType3(View view) {
            super(view);
            imageViewRight = (NetworkImageView) view.findViewById(R.id.picture_type3_iv_right);
            imageViewTop = (NetworkImageView) view.findViewById(R.id.picture_type3_iv_top);
            imageViewBottom = (NetworkImageView) view.findViewById(R.id.picture_type3_iv_bottom);
            textViewPictureNum = (TextView) view.findViewById(R.id.picture_type3_tv_counts);
            textViewName = (TextView) view.findViewById(R.id.picture_type3_tv_name);
            textViewReplyNum = (TextView) view.findViewById(R.id.picture_type3_tv_gentie);
        }
    }

}
